<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属小区" prop="estateId">
        <el-select v-model="queryParams.estateId" placeholder="请选择小区" style="width:140px;" size="mini" filterable clearable>
          <el-option v-for="estate in estatesOptions"
                     :key="estate.id"
                     :label="estate.estateName"
                     :value="estate.id"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport"
                   v-hasPermi="['system:log:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-row style="margin-top: 20px" :gutter="20">
      <el-col class="biankuang" :span="4">
        <div style="margin-top: 20px;font-size: 50px;margin-left: 10px;color: #3aa4ff">
            {{this.userNum}}
        </div>
        <div style="font-size: 20px;margin-left: 10px">
          处理人
        </div>
        <div style="background-color: #3aa4ff;width: 100%;height: 5px;margin-top: 20px"/>
      </el-col>
      <el-col class="biankuang" :span="4">
        <div style="margin-top: 20px;font-size: 50px;margin-left: 10px;color: #33cea0">
          {{this.taskNum}}
        </div>
        <div style="font-size: 20px;margin-left: 10px">
          总任务数
        </div>
        <div style="background-color: #33cea0;width: 100%;height: 5px;margin-top: 20px"/>
      </el-col>
      <el-col class="biankuang" :span="4">
        <div style="margin-top: 20px;font-size: 50px;margin-left: 10px;color: #FFA20A">
          {{this.beganNum}}
        </div>
        <div style="font-size: 20px;margin-left: 10px">
          未开始任务
        </div>
        <div style="background-color: #FFA20A;width: 100%;height: 5px;margin-top: 20px"/>
      </el-col>
      <el-col class="biankuang" :span="4">
        <div style="margin-top: 20px;font-size: 50px;margin-left: 10px;color: red">
          {{this.afootNum}}
        </div>
        <div style="font-size: 20px;margin-left: 10px">
          进行中任务
        </div>
        <div style="background-color:red;width: 100%;height: 5px;margin-top: 20px"/>
      </el-col>
      <el-col class="biankuang" :span="4">
        <div style="margin-top: 20px;font-size: 50px;margin-left: 10px;color: #6379bb">
          {{this.completedNum}}
        </div>
        <div style="font-size: 20px;margin-left: 10px">
          已完成任务
        </div>
        <div style="background-color: #6379bb;width: 100%;height: 5px;margin-top: 20px"/>
      </el-col>
    </el-row>

    <!--总统计表-->
    <span style="font-size: 30px;margin-top: 20px">总统计表</span>
    <el-table v-loading="loading" :data="allList" style="">
      <el-table-column label="处理人" align="center" prop="realName" />
      <el-table-column label="任务总数" align="center" prop="count" />
      <el-table-column label="进行中" align="center" prop="afoot" />
      <el-table-column label="已完成" align="center" prop="completed" />
    </el-table>
  </div>
</template>

<script>
import { listRecordCount } from "@/api/module/repairRecord";
import {listAllEstate} from "@/api/module/estate/estate";

export default {
  name: "Log",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      // loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工作日志表格数据
      allList: [],
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      estatesOptions:[],
      colFields:[
        "realName",
        "completed",
        "planName",
        "taskInfoName",
        "endTime",
      ],
      // 查询参数
      queryParams: {
        estateId:undefined,
        eventType:4,
        beginTime:undefined,
        endTime:undefined,
      },
      // 表单参数
      form: {},
      spanArr: [],
      // 表单校验
      rules: {},
      userNum: 0,
      taskNum: 0,
      beganNum: 0,
      afootNum: 0,
      completedNum: 0,
    }
  },
  created() {
    this.getEstateList();
    this.getList();
    this.getSpanArr();
  },
    methods: {
    /** 查询工作列表 */
    getList() {
      this.loading = true;
      listRecordCount(this.queryParams).then(response => {
        console.log(response)
        this.allList = response.userPlanList;
        this.userList = response.userPlanInfoList;
        this.userNum = response.userCount;
        this.taskNum = response.planCount;
        this.beganNum = response.began;
        this.afootNum = response.afoot;
        this.completedNum = response.completed;
        this.loading = false;
      });
    },
    getSpanArr() {

      for (let i = 0; i < this.userList.length; i++) {
        let row = i;
        // let col = i % this.colCount;
        if (row === 0) {
          // i 表示行 j表示列
          for (let j = 0; j < this.colFields.length; j++) {
            this.spanArr[i * this.colFields.length + j] = {
              rowspan: 1,
              colspan: 1,
            };
          }
        } else {
          for (let j = 0; j < this.colFields.length; j++) {
            // 当前和上一次的一样
            // 1. 合并所有列的相同数据单元格
            if (
              this.colFields[j] == "realName" ||
              this.colFields[j] == "completed"
            ){
              if (
                this.userList[row][this.colFields[j]] ===
                this.userList[row - 1][this.colFields[j]]
              ) {
                let beforeItem =
                  this.spanArr[(row - 1) * this.colFields.length + j];
                this.spanArr[row * this.colFields.length + j] = {
                  rowspan: 1 + beforeItem.rowspan, // 合并几行
                  colspan: 1, // 合并几列,我这里只是跨行合并,不跨列合并,所以用的1
                };
                beforeItem.rowspan = 0;
                beforeItem.colspan = 0;
              } else {
                // rowspan 和 colspan 都为1表格此单元格不合并
                this.spanArr[row * this.colFields.length + j] = {
                  rowspan: 1,
                  colspan: 1,
                };
              }
            }

          }
        }
      }
      // 对数据进行倒序
      let stack = [];
      for (let i = 0; i < this.colFields.length; i++) {
        for (let j = 0; j < this.userList.length; j++) {
           console.log("i=" + i + " j=" + j);
          // i 表示列 j表示行
          if (j === 0) {
            if (this.spanArr[j * this.colFields.length + i].rowspan === 0) {
              stack.push(this.spanArr[j * this.colFields.length + i]);
            }
          } else {
            if (this.spanArr[j * this.colFields.length + i].rowspan === 0) {
              stack.push(this.spanArr[j * this.colFields.length + i]);
            } else {
              stack.push(this.spanArr[j * this.colFields.length + i]);
              while (stack.length > 0) {
                let pop = stack.pop();
                let len = stack.length;
                this.spanArr[(j - len) * this.colFields.length + i] = pop;
              }
            }
          }
        }
      }
       console.log("this.spanArr:"+this.spanArr);
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      console.log("this.spanArr:"+this.spanArr[rowIndex * this.colFields.length + columnIndex]);
      return this.spanArr[rowIndex * this.colFields.length + columnIndex];
    },
    getEstateList() {
      listAllEstate().then(response => {
        this.estatesOptions = response.data;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有工作量统计数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportLog(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  },
  updated() {
    this.getSpanArr();
  },
};
</script>
<style>
.biankuang{
  height: 150px;
  background-color: #eeeeee;
  border:solid 2px #aaaaaa;
  border-radius: 5px;
  margin-right: 30px
}
</style>
